package com.zhaofan.other;

import org.junit.Test;

import java.util.HashMap;

/**
 * 13. 罗马数字转整数
 * https://leetcode-cn.com/problems/roman-to-integer/
 */
public class No13 {

    @Test
    public void test(){
        System.out.println(romanToInt("IV"));
    }

    public  int romanToInt(String s) {
        HashMap<String, Integer> general = new HashMap<>();
        general.put("I",1);
        general.put("V",5);
        general.put("X",10);
        general.put("L",50);
        general.put("C",100);
        general.put("D",500);
        general.put("M",1000);
        int result=0,next=0;
        char[] array = s.toCharArray();
        for (int i = array.length - 1; i >= 0; i--) {
            int v= general.get(String.valueOf(array[i]));
            if(v>=next){
                result+=v;
            }else{
                result-=v;
            }
            next=v;
        }
        return result;
    }
}
